--[[
    数独房间实体
]]--

local blaze = require "blaze"
local game = require "club.threecard.sudoku"
local Room = require "entities.Room"

local log = blaze.logging.get("SudokuRoom")

local M = game.kbe.entity("SudokuRoom", Room)

function M:__init__()
    Room.__init__(self)
end
local phases = {
    [0] = "空闲",
    [1] = "准备",
    [2] = "解谜",
    [3] = "结算"
}

--[[ =============================
             服务端接口
================================== ]]--

--[[ =============================
             服务端回调
================================== ]]--

function M:on_phase(phase_id, countdown)
    local phase_name = phases[phase_id] or "未知"
    if countdown > 0 then
        log.debug(string.format("进入房间阶段：%s 倒计时%d毫秒", phase_name, countdown))
    else
        log.debug(string.format("进入房间阶段：%s", phase_name))
    end
    game.store.ingame.set_phase({ id = phase_id, countdown = countdown })
end

function M:on_settlement(info)
    log.debug(string.format("结算%s", blaze.dump(info)))
    local players = {}
    local statistics = {}
    for _, player_info in ipairs(info.players) do
        local player = game.kbe.find_entity(player_info.id)
        players[#players + 1] = player
        statistics[#statistics + 1] = player_info
    end
    local data = {
        winner = info.winner,
        players = players,
        statistics = statistics
    }
    game.store.settlement.settle(data)
    game.router.push("/club.threecard.sudoku/settlement")
end

return M